from wsgiref import headers

from utils.GetKeyWord import GetKeyWord
from utils.SendMethod import SendMethod


class MemberInterface:
    def __init__(self, url):
        self.url = url

    # 获取接口
    def get_auth_code(self, telephone):
        method = "get"
        url = self.url + "/sso/getAuthCode"
        payload = {"telephone": telephone}
        return SendMethod.sendmethod(method=method, url=url, params=payload)

    # 获取接口验证码
    def get_verify_code(self, telephone):
        response = self.get_auth_code(telephone)
        return GetKeyWord.get_keyword(response, "data")

    # 会员注册
    def register(self, payload):
        method = "post"
        url = self.url + "/sso/register"
        return SendMethod.sendmethod(method=method, url=url, data=payload)

    # 登录
    def login(self, username, password):
        method = "post"
        url = self.url + "/sso/login"
        payload = {
            "username": username,
            "password": password
        }
        return SendMethod.sendmethod(method=method, url=url, data=payload)

    # 提取token
    def get_member_token(self, username, password):
        response = self.login(username, password)
        token = GetKeyWord.get_keyword(response, "token")
        return {"Authorization": f"Bearer {token}"}

    # 刷新token
    def get_refresh_token(self, username, password):
        method = "get"
        url = self.url + "/sso/refreshToken"
        head = self.get_member_token(username, password)
        print(head)
        return SendMethod.sendmethod(method=method, url=url, headers=head)

    # 获取会员信息
    def get_vip_message(self, username, password):
        method = "get"
        url = self.url + "/sso/info"
        return SendMethod.sendmethod(method=method, url=url,params=username,
                                     headers=self.get_member_token(username, password))

    # 会员修改密码
    def get_amend_password(self, payload):
        method = "post"
        url = self.url + "/sso/updatePassword"
        return SendMethod.sendmethod(method=method, url=url, data=payload)


if __name__ == '__main__':
    # method = "post"
    url = "http://47.108.206.100:8085"
    member = MemberInterface(url)
    # print(member.get_auth_code("18569945662"))
    # print(member.get_verify_code("18569945662"))
    telephone = "18569945662"
    # # params = {"telephone": telephone}
    # payload = {
    #     'username': 'Anna',
    #     'password': 'anna123',
    #     'telephone': telephone,
    #     'authCode': member.get_verify_code(telephone)
    # }

    # print(member.register(payload))  # 注册
    username = "Anna"
    password = "anna123"
    print(member.login(username, password))  # 登录
    print(member.get_refresh_token(username,password))  # 刷新token
    print(member.get_vip_message(username, password))        # 获取会员信息
    # payload = {
    #     "password":"anna123",
    #     "telephone":telephone,
    #     "authCode":member.get_verify_code(telephone)
    # }
    # print(member.get_amend_password(payload))      # 修改密码
